﻿/*********************************************************/
/*   Kookie 0.1                                          */
/*   Plugin para recuperar valores de cookies            */
/*                                                       */
/*   Sintaxe $.kookie(nome_do_cookie, nome_da_chave)     */
/*                                                       */
/*********************************************************/

$(document).ready(function () {

    jQuery.kookie = function (cookieName, cookieKey) {

        var KeyValue = '';

        var cookie = document.cookie;
        cookie = charReplace(cookie);
        var ArrayElements = cookie.split(';');

        var element = '';
        for (var i = 0; i < ArrayElements.length; i++) {
            if (ArrayElements[i].toString().indexOf(cookieName) >= 0) {
                element = ArrayElements[i].toString();
                break;
            }
        }

        var match = '';
        match = element.toString().replace(cookieName + '=', '');
        var ArrayKeys = match.split('&');

        for (var i = 0; i < ArrayKeys.length; i++) {
            if (ArrayKeys[i].toString().replace('=', '_').indexOf(cookieKey) >= 0) {
                KeyValue = ArrayKeys[i];
                break;
            }
        }

        KeyValue = KeyValue.split('=')[1];
        return KeyValue;

    };

    function charReplace(string) {
        var stringReturn = string.replace(/%3D/g, '=');
        stringReturn = stringReturn.replace(/%5F/g, '_');
        stringReturn = stringReturn.replace(/%25/g, '_');
        stringReturn = stringReturn.replace(/%26/g, '&');
        stringReturn = stringReturn.replace(/%20/g, ' ');
        return stringReturn;
    }

});

